    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using System;

    namespace Traffic
    {
        /// <summary>
        /// Control is a DrawableGameComponent that draws a Control on the screen. It works
        /// differently on Xbox and Windows. On windows, this will be a Control that is
        /// controlled using both the mouse and the gamepad. On Xbox, the Control will be
        /// controlled using only the gamepad.
        /// </summary>
        public class Control : DrawableGameComponent
        {
            #region Fields and properties

            // the content manager is passed in in Control's constructor, and will be used to
            // load the texture for the Control to draw with.
            ContentManager content;

            // this spritebatch is created internally, and is used to draw the Control.
            SpriteBatch spriteBatch;

            // this is the sprite that is drawn at the current Control position.
            // textureCenter is used to center the sprite when drawing.
            string textureName;
            Texture2D controlTexture;
            Vector2 textureCenter;            

            Color controlColor;

            Cursor mouseCursor;
            protected ButtonState previousState;
            
            // Position is the Control position, and is in screen space. 
            private Vector2 position;
            public Vector2 Position
            {
                get { return position; }
            }

            private bool activated;
            public bool Activated
            {
                get { return activated; }
                set { activated = value; }
            }

            public void Clear()
            {
                Activated = false;
            }

            #endregion

            #region Creation and initialization

            // this constructor doesn't really do much of anything, just calls the base 
            // constructor, and saves the contentmanager so it can be used in
            // LoadGraphicsContent.
            public Control(Game game, ContentManager content, Cursor c, Vector2 pos, string texture)
                : base(game)
            {
                this.textureName = texture;
                this.content = content;
                this.position = pos;
                this.mouseCursor = c;
                this.previousState = ButtonState.Released;
            }

            public Control(Game game) : base(game) { }

            // LoadGraphicsContent needs to load the Control texture and find its center.
            // also, we need to create a SpriteBatch.
            protected override void LoadGraphicsContent(bool loadAllContent)
            {
                if (loadAllContent)
                {
                    controlTexture = content.Load<Texture2D>(textureName);
                    textureCenter = new Vector2(controlTexture.Width / 2, controlTexture.Height / 2);

                    spriteBatch = new SpriteBatch(GraphicsDevice);
                }

                base.LoadGraphicsContent(loadAllContent);
            }

            #endregion

            #region Draw

            // Draw is pretty straightforward: we'll Begin the SpriteBatch, Draw the Control,
            // and then End.
            public override void Draw(GameTime gameTime)
            {               
                spriteBatch.Begin(SpriteBlendMode.None);
                //BackgroundBatch.Begin(SpriteBlendMode.None);

                spriteBatch.Draw(controlTexture, Position, controlColor);

                spriteBatch.End();

                base.Draw(gameTime);
            }

            #endregion

            #region Update

            // Update gets the current gamepad state and mouse state and uses that data to
            // calculate where the Control's position is on the screen. On xbox, the position
            // is clamped to the viewport so that the Control can't go off the screen. On
            // windows, doing something like that would be rude :)
            public override void Update(GameTime gameTime)
            {                
                if (MouseOver())
                {
                    controlColor = Color.Red;

                    if (previousState == ButtonState.Released && Mouse.GetState().LeftButton == ButtonState.Pressed)
                    {
                        activated = true;
                    }
                    previousState = Mouse.GetState().LeftButton;                    
                }
                else
                {
                    controlColor = Color.White;
                }
                
                base.Update(gameTime);
            }

            #endregion

          public bool MouseOver()
          {              
              Point p = new Point((int)mouseCursor.Position.X, (int)mouseCursor.Position.Y);

              Rectangle r = new Rectangle((int)Position.X, (int)Position.Y, (int)controlTexture.Width,(int)controlTexture.Height);

              if (r.Contains(p))
              {
                  return true;
              }
              else
              {
                  return false;
              }
         
        }
    }
}
